package com.ndd.service.impl;

import com.ndd.dto.AddressDTO;
import com.ndd.entity.Address;
import com.ndd.enums.ServiceCode;
import com.ndd.exception.ServiceException;
import com.ndd.mapper.AddressMapper;
import com.ndd.service.AddressService;
import com.ndd.vo.AddressVO;
import com.ndd.vo.UserLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 2024/4/27 15:25
 * wuhon
 */
@Service
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 根据用户id获取用户列表数据
     * @param userLoginVO
     * @return
     */
    @Override
    public List<AddressVO> getAddressListByUserId(UserLoginVO userLoginVO) {
        List<AddressVO> addressVOS = addressMapper.selectAll(userLoginVO.getId());
        if (addressVOS == null || addressVOS.size() == 0){
            //地址列表为空或者集合长度为0
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"该用户没有地址数据");
        }
        //地址列表不为空
        return addressVOS;
    }

    /**
     * 新增地址信息
     * @param addressDTO
     */
    @Override
    public void save(AddressDTO addressDTO,UserLoginVO userLoginVO) {
        //创建实体类
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO,address);
        address.setUserId(userLoginVO.getId());
        address.setCreatedUser(userLoginVO.getUsername());
        System.out.println(address.toString());
        //将数据插入到数据库
        addressMapper.insert(address);
    }
}
